//
//  Soldier.m
//  TowerDefence
//
//  Created by K3 on 6/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Soldier.h"
#import "GameLayer.h"
#import "PlayScreen.h"
#import "iCoreGUIController.h"

@implementation Soldier

@synthesize standingPosition, targetLocked;

-(id)initSoldierWith:(GameLayer*)_gameLayer {
    self=[super initCharacterWith:_gameLayer];
    inBattle=NO;
    lockTarget=NO;
    standingPosition=ccp(0,0);
    return self;
}

-(void)update:(ccTime)_timeUpdate {
    if(HP>0){
        if(lockTarget) {
            //NSLog(@"Lock Target");
            if ([handler.listGameObject containsObject:targetLocked]){
                //if(CGRectIntersectsRect([self rect], [targetLocked rect])){
                if (ccpDistance(spriteBody.position, targetLocked.spriteBody.position)<=10){
                    if (!inBattle)
                    {
                        //                    NSLog(@"Begin Attack");
                        [spriteBody stopAllActions];
                        [self attack];
                    }
                }
                else{
                    //edited recently
                    if(ccpDistance(standingPosition, targetLocked.spriteBody.position)>attackRange){
                        lockTarget=NO;
                    }
                    else{
                        //NSLog(@"Moving to target");
                        [self moveToPoint:targetLocked.spriteBody.position];
                    }
                    //end edited recently
                }
            }
            else{
                lockTarget=NO;
                inBattle=NO;
            }
            
        } else{
            //edit recently
            if (ccpDistance(spriteBody.position, standingPosition)>=5){
                //                NSLog(@"Moving Back");
                [self moveToPoint:standingPosition];
            }
            else{
                //               NSLog(@"Finding Target");
                [spriteBody stopAllActions];
                direction=NO_DIRECTION;
                [self findAndLockTarget];
            }
        }
    }
    else{
        targetLocked.inBattle=NO;
        targetLocked.direction=NO_DIRECTION;
        if(!die)
            [self animationDie];
    }
}

-(void)findAndLockTarget {
    for(int i = 0; i < handler.listGameObject.count; i++) {
        if([[handler.listGameObject objectAtIndex:i] isKindOfClass:[Enemy class]]) {
            Enemy *enemy=(Enemy*)[handler.listGameObject objectAtIndex:i];
            if(ccpDistance(standingPosition, enemy.spriteBody.position) <= attackRange && !enemy.fly) {
                targetLocked = enemy;
                [targetLocked.spriteBody stopAllActions];
                lockTarget = YES;
                break;
            }
        }
    }
}

-(float)distanceFromEnemy:(Enemy*)_enemy{
    return sqrtf(powf(spriteBody.position.x-_enemy.spriteBody.position.x,2)+powf( spriteBody.position.y-_enemy.spriteBody.position.y,2));
}

-(void)attack {   
    if([handler.listGameObject containsObject:targetLocked]) {
        if(!inBattle) {
            targetLocked.inBattle=YES;
            [self animationBattle];
            [targetLocked animationBattle];
        }
    }
    else{
        lockTarget=NO;
        inBattle=NO;
    }
}




-(void)battle {
    if ([handler.listGameObject containsObject:targetLocked]){
        if(!handler.mainGUIController.isMuteSound) {
            [handler.mainGUIController playEffect:AUDIO_SOLDIER_ATTACK];
        }
        
        [targetLocked animationHit];
        targetLocked.HP-=damage;
        [targetLocked updateBloodMeter];
    }
    else{
        lockTarget=NO;
        inBattle=NO;
    }
}

-(void)moveToPoint:(CGPoint)_CGPoint{
    if(lockTarget && inBattle) {
        lockTarget = NO;
        inBattle = NO;
    }
    
    CGPoint vector=ccp(_CGPoint.x-spriteBody.position.x ,_CGPoint.y-spriteBody.position.y);
    //calculate the direction of the soldier
    if(vector.y == 0){ 
        vector.y = 0.000001f; 
    }
    float radians = atan(vector.x/vector.y);
    if(vector.y < 0){ 
        radians += PI;
    }
    CGPoint normalVector = ccp(sin(radians), cos(radians));
    spriteBody.position=ccp(spriteBody.position.x+normalVector.x*speed,spriteBody.position.y+normalVector.y*speed);
    //calculate the degree of the enemy's direction to see which direction he's using
    float degrees = [self radiansToDegrees:radians];
    //NSLog(@"The degree is %f",degrees);
    int nextDirection=NO_DIRECTION;
    if(degrees >= 0.0 && degrees < 180.0){
        nextDirection = DIRECTION_LEFT;
    }else{
        nextDirection = DIRECTION_RIGHT;
    }
    //if the soldier's direction is different from his current direction then change animation
    if(nextDirection!=direction){
        [self loadAnimationBasedOnDirection:nextDirection];
        direction=nextDirection;
    }
}

-(void)animationBattle{
    [super animationBattle];
    targetLocked.opponent=self;
    inBattle=YES;
    targetLocked.inBattle=YES;
}

@end
